﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ECCIT.Extension.Weibo_v2.Model;
using ECCIT.Extension.Weibo_v2.Common;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;
using ECCIT;
using OgilvyOne.UKNow.Mobile.Model.Uploads;
using OgilvyOne.UKNow.Configuration;
using OgilvyOne.UKNow.Mobile.Utilities;

namespace OgilvyOne.UKNow.Mobile.APIs.Uploads
{
    public class GetList : IHttpHandler
    {
        int errorcode = 0;
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string sPageSize = context.Request.Params["page_size"];
                string sCurrPageIndex = context.Request.Params["current_page"];
                int uid = int.Parse(context.Request.Params["uid"].ToString());
                int pageSize = 10;
                if (!string.IsNullOrEmpty(sPageSize))
                    pageSize = int.Parse(sPageSize);

                int currPageIndex = 0;
                if (!string.IsNullOrEmpty(sCurrPageIndex))
                    currPageIndex = int.Parse(sCurrPageIndex) - 1;
                getEventCommentImage(uid, pageSize, currPageIndex, context);
            }
            catch (Exception)
            {
                context.Response.Write(ResultUtility.ReturnResultJson("-300", "\"\"", "列表获取失败"));
            }
        }

        private void getEventCommentImage(int uid, int pagesize, int currpage, HttpContext context)
        {
            if (errorcode == 0)
            {
                EventsCommentBLL ecbll = new EventsCommentBLL(2052);
                PagingParameter pagingParams = PagingParameterFactory.CreateInstance(true, currpage, pagesize, null);
                PagingList pl = ecbll.GetEventsCommentsWithImageByUserID(uid, pagingParams);
                List<UploadImage> lui = new List<UploadImage>();
                if (pl.List != null)
                {
                    foreach (EventsCommentModel ecm in pl.List)
                    {
                        UploadImage ui = new UploadImage();
                        if (!string.IsNullOrEmpty(ecm.ImageName))
                        {

                            ui.Imgurl = ConfigCacheManager.DomainName +
                                        ConfigCacheManager.FilePath.GetFrontImageVirtualPath(ecm.ImageName);
                        }
                        else
                        {
                            ui.Imgurl = "";

                        }
                        ui.Types = "2";
                        ui.EventName = HttpUtility.HtmlEncode(ecm.Events.Title);
                        ui.EventID = ecm.EventsID;
                        lui.Add(ui);
                    }

                    UploadimgList uilist = new UploadimgList();
                    uilist.List = lui;
                    uilist.CurrentPageIndex = currpage + 1;
                    uilist.PageCount = pl.Capacity % pagesize == 0 ? pl.Capacity / pagesize : pl.Capacity / pagesize + 1;
                    uilist.TotalCount = pl.Capacity;
                    string s = JsonSerializationHelper.ObjectToJson(uilist);
                    context.Response.Write(ResultUtility.ReturnResultJson("0", s, "获取用户上传数据成功"));
                }
            }
            else
            {
                context.Response.Write(ResultUtility.ReturnResultJson(errorcode.ToString(), "", "获取用户上传数据失败"));
            }
        }
    }
}